#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
生成火箭图标
需要安装: pip install pillow
"""

from PIL import Image, ImageDraw
import os

def create_rocket_icon(size=256):
    """创建一个火箭图标"""
    # 创建透明背景
    img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 定义颜色
    rocket_body = (100, 150, 255)  # 蓝色
    rocket_window = (200, 220, 255)  # 浅蓝色
    rocket_fin = (70, 120, 220)  # 深蓝色
    fire_outer = (255, 100, 50)  # 橙红色
    fire_middle = (255, 180, 50)  # 橙黄色
    fire_inner = (255, 230, 100)  # 黄色
    
    # 缩放因子
    scale = size / 256
    
    def s(x):
        """缩放坐标"""
        return int(x * scale)
    
    # 绘制火箭尾焰（外层）
    fire_points_outer = [
        (s(128), s(180)),  # 顶点
        (s(100), s(220)),  # 左下
        (s(118), s(200)),  # 左中
        (s(128), s(240)),  # 底部中心
        (s(138), s(200)),  # 右中
        (s(156), s(220)),  # 右下
    ]
    draw.polygon(fire_points_outer, fill=fire_outer)
    
    # 绘制火箭尾焰（中层）
    fire_points_middle = [
        (s(128), s(185)),
        (s(110), s(215)),
        (s(128), s(225)),
        (s(146), s(215)),
    ]
    draw.polygon(fire_points_middle, fill=fire_middle)
    
    # 绘制火箭尾焰（内层）
    fire_points_inner = [
        (s(128), s(190)),
        (s(120), s(210)),
        (s(128), s(215)),
        (s(136), s(210)),
    ]
    draw.polygon(fire_points_inner, fill=fire_inner)
    
    # 绘制火箭机身（主体）
    body_left = s(100)
    body_right = s(156)
    body_top = s(80)
    body_bottom = s(180)
    draw.rectangle([body_left, body_top, body_right, body_bottom], fill=rocket_body)
    
    # 绘制火箭头部（三角形）
    nose_points = [
        (s(128), s(30)),  # 顶点
        (s(100), s(80)),  # 左下
        (s(156), s(80)),  # 右下
    ]
    draw.polygon(nose_points, fill=rocket_body)
    
    # 绘制火箭侧翼（左）
    left_fin = [
        (s(100), s(140)),  # 右上
        (s(70), s(160)),   # 左上
        (s(70), s(180)),   # 左下
        (s(100), s(180)),  # 右下
    ]
    draw.polygon(left_fin, fill=rocket_fin)
    
    # 绘制火箭侧翼（右）
    right_fin = [
        (s(156), s(140)),  # 左上
        (s(186), s(160)),  # 右上
        (s(186), s(180)),  # 右下
        (s(156), s(180)),  # 左下
    ]
    draw.polygon(right_fin, fill=rocket_fin)
    
    # 绘制窗户（圆形）
    window_center = (s(128), s(110))
    window_radius = s(15)
    draw.ellipse([
        window_center[0] - window_radius,
        window_center[1] - window_radius,
        window_center[0] + window_radius,
        window_center[1] + window_radius
    ], fill=rocket_window)
    
    # 绘制窗户高光
    highlight_radius = s(8)
    draw.ellipse([
        s(122), s(104),
        s(122) + highlight_radius,
        s(104) + highlight_radius
    ], fill=(255, 255, 255, 180))
    
    # 绘制装饰线条
    draw.line([body_left, s(85), body_right, s(85)], fill=rocket_fin, width=s(2))
    draw.line([body_left, s(175), body_right, s(175)], fill=rocket_fin, width=s(2))
    
    return img

def main():
    print("🚀 生成火箭图标...")
    print("=" * 50)
    
    # 定义输出路径
    windows_icon_path = "windows/runner/resources/app_icon.ico"
    macos_icon_path = "macos/Runner/Assets.xcassets/AppIcon.appiconset"
    linux_icon_path = "linux/runner/resources"
    
    # 生成不同尺寸的图标
    sizes = [16, 32, 48, 64, 128, 256]
    images = []
    
    for size in sizes:
        print(f"生成 {size}x{size} 图标...")
        img = create_rocket_icon(size)
        images.append(img)
    
    # 保存 Windows ICO 文件
    print(f"\n保存 Windows 图标: {windows_icon_path}")
    os.makedirs(os.path.dirname(windows_icon_path), exist_ok=True)
    images[0].save(
        windows_icon_path,
        format='ICO',
        sizes=[(s, s) for s in sizes]
    )
    print(f"✓ Windows 图标已生成")
    
    # 保存 PNG 文件（用于其他平台）
    print(f"\n保存 PNG 图标...")
    
    # Linux
    os.makedirs(linux_icon_path, exist_ok=True)
    icon_256 = create_rocket_icon(256)
    icon_256.save(f"{linux_icon_path}/app_icon.png", format='PNG')
    print(f"✓ Linux 图标已生成: {linux_icon_path}/app_icon.png")
    
    # macOS (保存各种尺寸)
    if os.path.exists(macos_icon_path):
        print(f"\n生成 macOS 图标...")
        macos_sizes = [16, 32, 64, 128, 256, 512, 1024]
        for size in macos_sizes:
            img = create_rocket_icon(size)
            img.save(f"{macos_icon_path}/icon_{size}x{size}.png", format='PNG')
            # 2x retina
            img_2x = create_rocket_icon(size * 2)
            img_2x.save(f"{macos_icon_path}/icon_{size}x{size}@2x.png", format='PNG')
        print(f"✓ macOS 图标已生成")
    
    # 保存一个大尺寸预览
    preview_img = create_rocket_icon(512)
    preview_img.save("icon_preview.png", format='PNG')
    print(f"\n✓ 预览图已保存: icon_preview.png")
    
    print("\n" + "=" * 50)
    print("🎉 图标生成完成！")
    print("\n提示:")
    print("  - Windows: windows/runner/resources/app_icon.ico")
    print("  - Linux: linux/runner/resources/app_icon.png")
    print("  - macOS: macos/Runner/Assets.xcassets/AppIcon.appiconset/")
    print("  - 预览: icon_preview.png")
    print("\n请重新编译应用程序以应用新图标。")

if __name__ == "__main__":
    try:
        main()
    except ImportError:
        print("❌ 错误: 未安装 Pillow 库")
        print("\n请先安装依赖:")
        print("  pip install pillow")
        print("\n或使用:")
        print("  pip3 install pillow")
        exit(1)
    except Exception as e:
        print(f"❌ 错误: {e}")
        import traceback
        traceback.print_exc()
        exit(1)

